using System;
using System.Data;
using System.Xml;

using OR.Reuse.Persistence;

using Derived = OR.Lazarus.Library.CPP.Rules;

namespace OR.Lazarus.Library.CPP.Rules.Lazarus
{
	/// <summary>
	/// Describes a set of rules for naming classes, fields and methods.
	/// </summary>
   public class RulesetDetail : EntityBase
   {
		protected System.String mId;
		protected string mDescription;
	
		protected Derived.IncludeCollection mIncludes;
		protected Derived.ClassRuleCollection mClassRules;
		protected Derived.VariableRuleCollection mVariableRules;
		protected Derived.MethodRuleCollection mMethodRules;

		public RulesetDetail()
		{
			mIncludes = new Derived.IncludeCollection();
			mIncludes.ParentEntity = this;
			mClassRules = new Derived.ClassRuleCollection();
			mClassRules.ParentEntity = this;
			mVariableRules = new Derived.VariableRuleCollection();
			mVariableRules.ParentEntity = this;
			mMethodRules = new Derived.MethodRuleCollection();
			mMethodRules.ParentEntity = this;
	
		}
	
		/// <summary>
		/// 
		/// </summary>
		public System.String Id
		{
			get
			{
				return ( mId );
			}
			set
			{
				mId = value;
			}
		}

		/// <summary>
		/// 
		/// </summary>
		public string Description
		{
			get
			{
				return ( mDescription );
			}
			set
			{
				mDescription = value;
			}
		}

		public Derived.IncludeCollection Includes
		{
			get
			{
				return ( mIncludes );
			}
			set
			{
				mIncludes = value;
			}
		}

		public Derived.ClassRuleCollection ClassRules
		{
			get
			{
				return ( mClassRules );
			}
			set
			{
				mClassRules = value;
			}
		}

		public Derived.VariableRuleCollection VariableRules
		{
			get
			{
				return ( mVariableRules );
			}
			set
			{
				mVariableRules = value;
			}
		}

		public Derived.MethodRuleCollection MethodRules
		{
			get
			{
				return ( mMethodRules );
			}
			set
			{
				mMethodRules = value;
			}
		}

		public override void LoadXml( XmlElement pSource )
		{
			// Scalar Values
			mId = DataUtility.GetString( pSource, "id" );
			XmlElement vDescription = ( XmlElement )pSource.SelectSingleNode( "lzr:description", PersistenceConfig.Namespaces );
			
			if ( vDescription != null )
			{
				mDescription = vDescription.InnerText;
			}
			
			XmlElement vIncludes = ( XmlElement )pSource.SelectSingleNode( "lzr:includes", PersistenceConfig.Namespaces );
			
			if ( vIncludes != null )
			{
				mIncludes.LoadXml( vIncludes );
			}
			XmlElement vClassRules = ( XmlElement )pSource.SelectSingleNode( "lzr:class-rules", PersistenceConfig.Namespaces );
			
			if ( vClassRules != null )
			{
				mClassRules.LoadXml( vClassRules );
			}
			XmlElement vVariableRules = ( XmlElement )pSource.SelectSingleNode( "lzr:variable-rules", PersistenceConfig.Namespaces );
			
			if ( vVariableRules != null )
			{
				mVariableRules.LoadXml( vVariableRules );
			}
			XmlElement vMethodRules = ( XmlElement )pSource.SelectSingleNode( "lzr:method-rules", PersistenceConfig.Namespaces );
			
			if ( vMethodRules != null )
			{
				mMethodRules.LoadXml( vMethodRules );
			}
		}
		
		public override XmlElement SaveXml( XmlDocument p_docContext )
		{
			XmlElement vNewElement	= p_docContext.CreateElement( "lzr", "ruleset", "http://schemas.icitadel.com/Lazarus/CPP/Rules.xsd" );

			// Scalar Values
			if ( !String.IsNullOrEmpty( mId ) )
			{
				vNewElement.SetAttribute( "id", mId );
			}
					
			if ( !String.IsNullOrEmpty( mDescription ) )
			{
				XmlElement	vDescription = p_docContext.CreateElement( "lzr", "description", "http://schemas.icitadel.com/Lazarus/CPP/Rules.xsd" );
				vDescription.InnerText = mDescription;
				vNewElement.AppendChild( vDescription );
			}
					

			// Collections
			
			XmlElement	vCollection	= null;
			
			vCollection = mIncludes.SaveXml( p_docContext );
			if ( vCollection != null )
			{
				vNewElement.AppendChild( vCollection );
			}
			
			vCollection = mClassRules.SaveXml( p_docContext );
			if ( vCollection != null )
			{
				vNewElement.AppendChild( vCollection );
			}
			
			vCollection = mVariableRules.SaveXml( p_docContext );
			if ( vCollection != null )
			{
				vNewElement.AppendChild( vCollection );
			}
			
			vCollection = mMethodRules.SaveXml( p_docContext );
			if ( vCollection != null )
			{
				vNewElement.AppendChild( vCollection );
			}

			return ( vNewElement );
		}

		public virtual object Clone()
		{
			Derived.RulesetDetail v_oReturn = new Derived.RulesetDetail();
			
			v_oReturn.Id = mId;
			v_oReturn.Description = mDescription;
			
			// FK Values

			// Collections

			foreach ( Derived.IncludeDetail vDetail in mIncludes )
			{
				v_oReturn.Includes.Add( vDetail.CloneTyped() );
			}

			foreach ( Derived.ClassRuleDetail vDetail in mClassRules )
			{
				v_oReturn.ClassRules.Add( vDetail.CloneTyped() );
			}

			foreach ( Derived.VariableRuleDetail vDetail in mVariableRules )
			{
				v_oReturn.VariableRules.Add( vDetail.CloneTyped() );
			}

			foreach ( Derived.MethodRuleDetail vDetail in mMethodRules )
			{
				v_oReturn.MethodRules.Add( vDetail.CloneTyped() );
			}
			
			return ( v_oReturn );
		}

		public virtual Derived.RulesetDetail CloneTyped()
		{
			return ( ( Derived.RulesetDetail )this.Clone() );
		}
   }
}
   